<?php
function random_float ($min,$max) {
	return ($min+lcg_value()*(abs($max-$min)));
}
// Configuration
//http://www.franzone.com/2008/05/20/how-do-i-get-meta-data-from-mysql-using-php/

$host = 'localhost';
$user = 'root';
$pass = '';
$database = 'cc_v2';
$numberOfRecs = 500;
$countries = array("Australia", "Bangladesh", "England", "India", "New Zealand", "Pakistan", "South Africa", "Sri Lanka", "West Indies", "Zimbabwe");
$playerTypes = array("Batsman","Bowler", "AllRounder","WicketKeeper");
$statTypes = array("T20","ODI", "TEST");


// Connect to the MySQL database


$conn = mysql_connect($host, $user, $pass) or die("Could not connect to database\n");
mysql_select_db($database, $conn) or die("Could not connect to $database\n");


$tableName="stat";

$cols = mysql_query("SHOW COLUMNS FROM $tableName", $conn);
if ($cols) {
	$output = array();
	while ($col = mysql_fetch_assoc($cols)) {
		$output[$col['Field']] = $col['Type'];
	}
	mysql_free_result($cols);
}
echo "TRUNCATE $database.$tableName;<br>";
for ( $index = 1; $index <= $numberOfRecs; $index++ ) {
	if(($index%50)<21){//Batsman
		$playerTypeIndex=0;
		$battingAvgMin=30;
		$battingAvgMax=50;
	}else if(($index%50)>20 && ($index%50)<36){//Bowler
		$playerTypeIndex=1;
		$battingAvgMin=5;
		$battingAvgMax=20;
	}else if(($index%50)>35 && ($index%50)<46){//All Rounder
		$playerTypeIndex=2;
		$battingAvgMin=20;
		$battingAvgMax=30;
	}else {// Wicket Keeper
		$playerTypeIndex=3;
		$battingAvgMin=20;
		$battingAvgMax=30;
	}
	$playerType=$playerTypes[$playerTypeIndex];
	for ( $index2 = 0, $max_count2 = sizeof( $statTypes ); $index2 < $max_count2; $index2++ ) {
		$matches=mt_rand(0,300);
		if($index2==0 || $index2==1){
			$innings=$matches;
		}else{
			$innings=($matches * mt_rand(100,200))/100;
		}
		$notOuts=($innings * mt_rand(5,10))/100;
		
		
		$strQuery = "Insert into $tableName (";
		foreach ($output as $field => $type) {
			if(strcasecmp($field,"id")!=0){
				
				$strQuery = $strQuery."$field, ";
			}
			
			
		}
		$strQuery = substr($strQuery , 0, -2);
		$strQuery = $strQuery.") values (";
		foreach ($output as $field => $type) {
			if(strcasecmp($field,"id")!=0){
				if(strpos($field, 'Type')>0){
					$strQuery = $strQuery."'$statTypes[$index2]', ";
				} else if (strcasecmp($field,"playerId")==0) {
					$strQuery = $strQuery."'$index', ";
				} else if (strpos($type, 'loat')==1) {
					$number= round(random_float(0,10),2);
					$strQuery =$strQuery. " '" .$number. "' ,";
				} else if (strpos($type, 'nt')==1) {
					$number= mt_rand(0,1000);
					$strQuery =$strQuery . " '" .$number. "' ,";
				}
			}
			
			
		}
		$strQuery = substr($strQuery , 0, -2);
		$strQuery = $strQuery.");";
		echo "$strQuery<br>";
	}
}
if ($conn)
	mysql_close($conn);

?>